// pages/pay/pay.js
var app = getApp();
var address = require('../../utils/city.js')
var conmon = require('../../utils/conmon.js')
Page({
  data: {
    getcodes: "获取验证码",
    canGetCode: false,
    areaInfo: "请选择",
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    queryId: '',
    provinceId:''
  },
  onLoad: function (options) {
    var that = this;
    if (options.direct) {
      this.setData({
        isDirect: options.direct,
      })
    }
    var that = this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        //console.log(res)
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          addressDetaile: app.addressDetailes
        });
      }
    });
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          userName: options.name,
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          merchantType: options.id,
        });
      }
    });
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    // 默认联动显示北京
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
  },

  onReady: function () {
    // 页面渲染完成
    // console.log(wx.getStorageSync('countdowns'))
    wx.setStorageSync('countdowns', 60)
  },
  onShow: function () {
    // 页面显示
    if (wx.getStorageSync('youu') == 11) {//如果倒计时的时间还没好，就让他继续倒计时，当cookie等于1时不能倒计时，为2可以
      wx.setStorageSync("cookie", 2)
    }
    // else if (wx.getStorageSync('youu') == 22) {
    //   wx.setStorageSync("cookie", 2)
    // }
    else {
      wx.setStorageSync("cookie", 2)
    }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
    wx.setStorageSync('countdowns', this.data.countdowns);
    wx.setStorageSync('cookie', 1);
  },
  onShareAppMessage: function (res) {
    return {
      title: '品德共享酒庄',
      path: '/pages/index/index?id=' + wx.getStorageSync("userid"),
    }
  },
  inputUser: function (e) {
    if (e.detail.value != '') {
      this.setData({
        inputUser: e.detail.value
      })
    }
  }, inputAddress: function (e) {
    if (e.detail.value != '') {
      this.setData({
        inputAddress: e.detail.value
      })
    }
  }, inputPhone: function (e) {
    if (e.detail.value != '') {
      this.setData({
        inputPhone: e.detail.value
      })
    }

  },
  IDCardInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        IDCardInput: e.detail.value
      })
    }

  },
  returnPay: function () {
    //console.log(111)
    var that = this;
    var superID;
    var reg = /^1[34578]\d{9}$/
    if (that.data.inputPhone && that.data.inputAddress && that.data.inputUser && that.data.IDCardInput) {
      if (!reg.test(that.data.inputPhone)) {
        wx.showToast({
          title: "请输入正确的手机号",
          icon: 'loading',
          duration: 2000
        })
        return;
      }
      if (conmon.IdentityCodeValid(that.data.IDCardInput) != true) {
        wx.showToast({
          title: "请输入正确的身份证号",
          icon: 'loading',
          duration: 2000
        })
        return;
      }
      if (that.data.areaInfo == "请选择") {
        wx.showToast({
          title: "请选择所在地区",
          icon: 'loading',
          duration: 2000
        })
        return;
      }

    } else {
      wx.showToast({
        title: '请填写完整信息!',
        icon: "loading"
      })
      return;
    }
    wx.request({
      url: app.globalData.allUrl + "/sMSVerificationAction/verificationCode",
      data: {
        userId: wx.getStorageSync('userid'),
        code: that.data.inputGetCode,
        phone: that.data.inputPhone,
        addjson: {
          userId: wx.getStorageSync('userid'),
          phone: that.data.inputPhone,
          consignee: that.data.inputUser,
          expressageAdd: that.data.inputAddress,
          locationAdd: that.data.areaInfo,
          provinceId: that.data.provinceId,
          superName: that.data.IDCardInput,
          limits:20000,
         // longitudeNum: wx.getStorageSync('longitude'),
         // latitudeNum: wx.getStorageSync('latitude'),
          superId: -1,
        }
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        console.log(res.data)
        if (res.data) {
          wx.setStorageSync('addressID', res.data.id)
          wx.setStorageSync('addInputUser', res.data.consignee);
          wx.setStorageSync('addInputAddress', res.data.expressageAdd);
          wx.setStorageSync('addInputPhone', res.data.phone);
          wx.setStorageSync('addLocationAdd', res.data.locationAdd)
          if (that.data.isDirect == 1) {
            var addDirectAddress = {
              consignee: res.data.consignee,
              phone: res.data.phone,
              expressageadd: res.data.expressageAdd,
              locationadd: res.data.locationAdd,
              id: res.data.id,
              provinceid: parseInt(res.data.provinceId),
              idcard: res.data.superName,
            }
            console.log(res.data.provinceId)
            wx.setStorageSync('checkboxchooseaddress', addDirectAddress)
          }
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

  , addressInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        addressInput: e.detail.value
      })
    }
  }
  , areaInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        areaInput: e.detail.value
      })
    }
  }
  , priceInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        priceInput: e.detail.value
      })
    }
  }
  , realInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        realInput: e.detail.value
      })
    }
  }
  , timeInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        timeInput: e.detail.value
      })
    }
  }
  , phoneInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        phoneInput: e.detail.value
      })
    }
  }
  ,
  cityChange: function (e) {
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name  + that.data.citys[value[1]].name + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
      queryId: that.data.citys[value[1]].id,
      provinceId: parseInt(that.data.provinces[value[0]].id.slice(0, 2))
    })
    console.log(typeof (that.data.provinceId) )
  },
  hideCitySelected: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  startAnimation: function (isShow, offset) {
    var that = this
    var offsetTem
    if (offset == 0) {
      offsetTem = offset
    } else {
      offsetTem = offset + 'rpx'
    }
    this.animation.translateY(offset).step()
    this.setData({
      animationData: this.animation.export(),
      isVisible: isShow
    })
  },
  // 选择状态按钮
  selectState: function (e) {
    console.log('selectState1')
    this.startAnimation(false, -200)
    var status = e.currentTarget.dataset.status
    this.setData({
      status: status
    })
    console.log(this.data)

  }

})









